//
//  RBEmptyView.m
//  RMMaterialRed
//
//  Created by XBWH on 2024/3/13.
//

#import "RBEmptyView.h"

@implementation RBEmptyView

- (UIView *)rb_showEmptyWithBaseView:(UIView *)baseView OriginY:(CGFloat)orginY ImgPaht:(NSString *)imgPath Title:(NSString *)title SubTitle:(NSString *)subTitle{
    [self removeFromSuperview];
    self.frame = baseView.frame;
    [baseView addSubview:self];
    
    UIView *emptyBgView = [[UIView alloc]init];
    emptyBgView.frame = self.frame;
    [self addSubview:emptyBgView];
    
    UIImageView *iconImageView = [[UIImageView alloc]init];
    iconImageView.contentMode = UIViewContentModeScaleAspectFit;
    iconImageView.image = [UIImage imageNamed:imgPath];
    [emptyBgView addSubview:iconImageView];
    [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(emptyBgView.mas_centerY).mas_offset(-orginY);
        make.centerX.equalTo(emptyBgView);
    }];
    
    UILabel *titleLabael = [[UILabel alloc]init];
    titleLabael.font = RBPingFangFont(15);
    titleLabael.textColor = RBColor(179, 179, 179);
    titleLabael.text = title;
    [emptyBgView addSubview:titleLabael];
    
    UILabel *subTitleLabael = [[UILabel alloc]init];
    subTitleLabael.font = RBPingFangFont(10);
    subTitleLabael.textColor = RBColor(179, 179, 179);
    subTitleLabael.text = subTitle;
    [emptyBgView addSubview:subTitleLabael];

    [titleLabael mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(iconImageView);
        make.top.equalTo(iconImageView.mas_bottom).mas_offset(15);
    }];
    [subTitleLabael mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(iconImageView);
        make.top.equalTo(titleLabael.mas_bottom).mas_offset(RBScaleWidth(5));
    }];
    if (RBStringIsEmpty(subTitle)) {
        subTitleLabael.hidden = YES;
    }
    
    return emptyBgView;
}
- (UIView *)rb_showEmptyWithBaseView:(UIView *)baseView OriginY:(CGFloat)orginY ImageName:(NSString *)imageName Title:(NSString *)title isShowLoadBtn:(BOOL)isShowLoadBtn{
    self.frame = baseView.frame;
    [baseView addSubview:self];
    
    UIView *emptyBgView = [[UIView alloc]init];
    emptyBgView.frame = self.frame;
    [self addSubview:emptyBgView];
    
    UIImageView *iconImageView = [[UIImageView alloc]init];
    iconImageView.contentMode = UIViewContentModeScaleAspectFit;
    if (RBStringIsEmpty(imageName)) {
        iconImageView.image = [UIImage imageNamed:@"qsy_zwxl"];
    } else {
        iconImageView.image = [UIImage imageNamed:imageName];
    }
    [emptyBgView addSubview:iconImageView];
    [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(emptyBgView.mas_centerY).offset(-orginY);
        make.centerX.equalTo(emptyBgView);
    }];
    
    UILabel *titleLabael = [[UILabel alloc]init];
    if (RBStringIsEmpty(title)) {
        titleLabael.text = @"暂无数据";
    }else{
        titleLabael.text = title;
    }
    titleLabael.font = RBPingFangFont(12);
    titleLabael.textColor = RBColor(179, 179, 179);
    [emptyBgView addSubview:titleLabael];
    
    [titleLabael mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(iconImageView);
        make.top.equalTo(iconImageView.mas_bottom).mas_offset(RBScaleWidth(10));
    }];
    
    UIButton *reloadButton = [UIButton buttonWithType:UIButtonTypeCustom];
    reloadButton.titleLabel.font = RBPingFangFont(16);
    [reloadButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [reloadButton setTitle:@"刷新" forState:UIControlStateNormal];
    [reloadButton setBackgroundImage:[UIImage imageNamed:@"qsy_bg"] forState:UIControlStateNormal];
    [reloadButton addTarget:self action:@selector(reloadButtonCallback) forControlEvents:UIControlEventTouchUpInside];
    if(isShowLoadBtn){
        reloadButton.hidden = NO;
    }else{
        reloadButton.hidden = YES;
    }
    [emptyBgView addSubview:reloadButton];
    [reloadButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(iconImageView);
        make.top.equalTo(titleLabael.mas_bottom).mas_offset(RBScaleWidth(15));
        make.height.mas_equalTo(RBScaleWidth(44));
        make.width.mas_equalTo(RBScaleWidth(160));
    }];
    
    return emptyBgView;
}

- (void)reloadButtonCallback{
    if (self.reloadButtonCallbackBlock) {
        self.reloadButtonCallbackBlock();
    }
}


@end
